<!DOCTYPE html>
<html>

<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=9; IE=8; IE=7; IE=EDGE">
	<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
	<title>个人注册</title>
	<link rel="shortcut icon" href="http://bj960928.3vzhuji.net/favicon.ico" type="image/x-icon">
    <link rel="stylesheet" type="text/css" href="../css/all.css" />
    <link rel="stylesheet" type="text/css" href="../css/pages-register.css" />
	<script src="../js/jquery.min.js"></script>
	<script src="../js/check.js"></script>
</head>

<body>
	<div class="register py-container ">
		<!--head-->
		<div class="logoArea">
			<a href="" class="logo"></a>
		</div>
		<!--register-->
		<div class="registerArea">
			<h3>注册新用户<span class="go">我有账号，去<a href="login.html" target="_blank">登陆</a></span></h3>
			<div class="info">
				<form action="" method="post" id="reg_form" class="sui-form form-horizontal">
					<div class="control-group">
						<label class="control-label">手机号：</label>
						<div class="controls">
							<input type="text" id="phone" autocomplete="off" name="phone" placeholder="请输入你的手机号" class="input-xfat input-xlarge">
							<span class="error"></span>
						</div>
					</div>
					<div class="control-group">
						<label for="inputPassword" class="control-label">验证码：</label>
						<div class="controls">
							<input type="text" id="code" autocomplete="off" name="code" placeholder="验证码" class="input-xfat input-xlarge" style="width:132px">
							<button type="button" class="btn-xlarge" id="dyMobileButton">发送验证码</button>
							<span class="error"></span>
						</div>
					</div>
					<div class="control-group">
						<label for="inputPassword" class="control-label">登录密码：</label>
						<div class="controls">
							<input type="password" id="password" name="password" placeholder="设置登录密码" class="input-xfat input-xlarge">
							<span class="error"></span>
						</div>
					</div>
					<div class="control-group">
						<label for="inputPassword" class="control-label">确认密码：</label>
						<div class="controls">
							<input type="password" id="repassword" name="repassword" placeholder="再次确认密码" class="input-xfat input-xlarge">
							<span class="error"></span>
						</div>
					</div>
					<div class="control-group">
						<label for="inputPassword" class="control-label">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</label>
						<div class="controls">
							<input name="m1" type="checkbox" value="2" checked=""><span>同意协议并注册《品优购用户协议》</span>
						</div>
					</div>
					<div class="control-group">
						<label class="control-label"></label>
						<div class="controls btn-reg">
							<a id="reg_btn" class="sui-btn btn-block btn-xlarge btn-danger reg-btn" href="javascript:;">完成注册</a>
						</div>
					</div>
				</form>
				<div class="clearfix"></div>
			</div>
		</div>
		<!--foot-->
		<div class="py-container copyright">
			<ul>
				<li>关于我们</li>
				<li>联系我们</li>
				<li>联系客服</li>
				<li>商家入驻</li>
				<li>营销中心</li>
				<li>手机品优购</li>
				<li>销售联盟</li>
				<li>品优购社区</li>
			</ul>
			<div class="address">地址：北京市昌平区建材城西路金燕龙办公楼一层 邮编：100096 电话：400-618-4000 传真：010-82935100</div>
			<div class="beian">京ICP备08001421号京公网安备110108007702
			</div>
		</div>
	</div>
</body>
<script>
	$(document).ready(function(){
		function checkCode(){
			return parseInt(Math.random()*9000)+1000;
		}
		let wait = null;
		$("#dyMobileButton").click(function(){
			if(!checkPhone()){
				return;
			}
			window.sessionStorage.removeItem("checkCode");
			window.sessionStorage.setItem("checkCode",checkCode());
			alert(window.sessionStorage.getItem("checkCode"));
			$(this).attr("disabled","disabled");
			let i = 60;
			wait = setInterval(function(){
				$("#dyMobileButton").html(`重新获取(${i})`);
				if(i<0){
					i=60;
					$("#dyMobileButton").removeAttr("disabled");
					$("#dyMobileButton").html(`发送验证码`);
					clearInterval(wait);
				}else{
					i--;
				}
			},1000);
		})

		//验证手机号
		function checkPhone(){
			let flag = true;
			let rephone = /^1[3456789]\d{9}$/;
			let phone = $("#phone");
			let err = phone.siblings("span");
			err.empty();
			err[0].setAttribute("class","");
			if(!checkIsNull(phone.val())){
				err.html("手机号不能为空！");
				err[0].setAttribute("class","errIcon error");
				flag = false;
			}else if(!checkFormat(phone.val(),rephone)){
				err.html("手机号格式错误!");
				err[0].setAttribute("class","errIcon error");
				flag = false;
			}else{
				$.ajax({
					type:"get",
					url:"http://localhost:3301/user?phone="+phone.val(),
					async:false,
					success:function(e){
						if(!$.isEmptyObject(e)){
							err.html("此手机号已注册！");
							err[0].setAttribute("class","errIcon error");
							flag = false;
						}else{
							err.html("此手机号可用！");
							err[0].setAttribute("class","correctIcon correct");
						}
					}
				})
			}
			return flag;
		}
		$("#phone").blur(function(){
			checkPhone();
		})
		//验证码
		function checkReCode(){
			let flag = true;
			let code = window.sessionStorage.getItem("checkCode");
			let recode = $("#code");
			let err = recode.siblings("span");
			err.empty();
			err[0].setAttribute("class","");
			if(!checkIsNull(code)){
				err.html("请发送验证码");
				err[0].setAttribute("class","errIcon error");
				flag = false;
			}else if(code!=recode.val()){
				err.html("验证码不正确！");
				err[0].setAttribute("class","errIcon error");
				flag = false;
			}
			return flag;
		}
		//验证密码
		function checkPassword(){
			let flag = true;
			let fm = /^\w{6,21}$/;
			let pwd = $("#password");
			let err = pwd.siblings("span");
			err.empty();
			err[0].setAttribute("class","");
			if(!checkIsNull(pwd)){
				err.html("密码不能为空！");
				err[0].setAttribute("class","errIcon error");
				flag = false;
			}else if(!fm.test(pwd.val())){
				err.html("密码格式不正确！");
				err[0].setAttribute("class","errIcon error");
				flag = false;
			}else{
				err.html("此密码可用！");
				err[0].setAttribute("class","correctIcon correct");
			}
			return flag;
		}
		$("#password").blur(function(){
			checkPassword();
		})
		//验证确认密码
		function checkRepassword(){
			let flag = true;
			let repwd = $("#repassword");
			let pwd = $("#password");
			let err = repwd.siblings("span");
			err.empty();
			err[0].setAttribute("class","");
			if(repwd.val()!=pwd.val()){
				err.html("两次密码不一致！");
				err[0].setAttribute("class","errIcon error");
				flag = false;
			}
			return flag;
		}
		$("#repassword").blur(function(){
			checkRepassword();
		})

		function checkAgreest(){
			let flag = true;
			if(!$("[name=m1]").prop("checked")){
				alert("请同意协议");
				flag = false;
			}
			return flag;
		}
		
		function phoneEncryption(p){
			if(typeof(p)!=String){
				p=String(p);
			}
			let pp = '';
			for(let i = 0;i<p.length;i++){
				if(i>2&&i<7){
					pp=pp+"*";
				}else{
					pp=pp+p[i];
				}
			}
			return pp;
		}

		function register(){
			if(checkPhone()&&checkReCode()&&checkPassword()&&checkRepassword()&&checkAgreest()){
				let phone = $("#phone").val();
				let password = $("#password").val();
				let createTime = new Date().getTime();
				let nickname = phoneEncryption(phone);
				let d = {
					"phone":phone,
					"password":password,
					"create_time":createTime,
					"nickname":nickname
				};
				$.ajax({
					type:"post",
					url:"http://localhost:3301/user",
					data:d,
					success:function(e){
						alert("注册成功，即将跳转到登录页面");
						location.href="login.html";
					},
					error:function(e){
						console.log(e);
						alert("注册失败");
					}
				})
			}
		}
		$("#reg_btn").click(function(){
			register();
		})
	})
</script>
</html>